package mju.cxf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import mju.cxf.dto.ProductCategoriesDTO;
import mju.cxf.dto.ProductDTO;
import mju.cxf.entity.Categories;
import mju.cxf.entity.Product;
import mju.cxf.entity.ProductSpecifications;
import mju.cxf.mapper.ProductMapper;
import mju.cxf.service.ProductCategoriesService;
import mju.cxf.service.ProductService;
import mju.cxf.service.ProductSpecificationsService;
import mju.cxf.utils.TimestampUpdaterUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductCategoriesService productCategoriesService;
    @Autowired
    private PhotoServiceImpl photoServiceImpl;
    @Autowired
    private ProductSpecificationsService specificationsService;
    /**
     * A查询商品信息
     * @return
     */
    @Override
    public Product selectProduct(int id){
        return this.baseMapper.selectById(id);
    }

    /**
     * A分页查询商品信息
     * @return boolean
     */
    @Override
    public Page<ProductDTO> selectPageByName(String name, Integer state,int current, int size) {
        Page<ProductDTO> page = new Page<>(current, size);
        return this.baseMapper.selectPageVo(page, name, state);
    }
    /**
     * A新增商品信息
     * @param productDTO
     * @return
     */
    @Override
    public boolean saveProduct(ProductDTO productDTO) {
        Product product = new Product();
//        productDTO.setState(1);
        productDTO.setDeleted(0);
        productDTO.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(productDTO, product);
        TimestampUpdaterUtils.saveUtil(product);
        int result = this.baseMapper.insert(product);
        if (result > 0) {
            // 获取自增长 ID
            Integer nextId = product.getId();
            //在商品分类表中插入商品的分类信息
            ProductCategoriesDTO productCategoriesDTO = new ProductCategoriesDTO();
            productCategoriesDTO.setProductId(nextId);
            productCategoriesDTO.setCategoriesId(productDTO.getCategoriesId());
            productCategoriesService.saveProductCategories(productCategoriesDTO);
            //保存图片
            if (productDTO.getImageUrl() != null) {
                photoServiceImpl.saveImages(productDTO.getImageUrl());
            }
            return true; // 插入成功
        }
        return false; // 插入失败
    }
    /**
     * A删除商品信息
     * @return boolean
     */
    @Override
    public boolean deleteProduct(Integer id) {
        Product selected = this.selectProduct(id);
        productCategoriesService.deleteProductCategories(id);
        if ( this.removeById(id)){
            //删除oss图片
            photoServiceImpl.deleteUrl(selected.getImageUrl());
            return true;
        }
        return false;
    }
    /**
     * A修改商品信息
     * @return boolean
     */
    @Override
    public boolean updateProduct(ProductDTO productDTO) {
        Product product = new Product();
        productDTO.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(productDTO,product);
        productCategoriesService.updateProductCategories(productDTO.getId(), productDTO.getCategoriesId());
        Product selected = this.selectProduct(productDTO.getId());

        LambdaUpdateWrapper<Product> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper
                .eq(Product::getId, product.getId())
                .set(Product::getImageUrl, product.getImageUrl());

        if (this.update(product, updateWrapper)){
            if (productDTO.getImageUrl() == null || !(Objects.equals(selected.getImageUrl(), productDTO.getImageUrl()))) {
                //删除oss图片插入新图
                photoServiceImpl.deleteUrl(selected.getImageUrl());
                photoServiceImpl.saveImages(productDTO.getImageUrl());
                LambdaQueryWrapper<ProductSpecifications> lqw = new LambdaQueryWrapper<>();
                specificationsService.list(lqw.eq(ProductSpecifications::getProductId,productDTO.getId()))
                        .forEach(specifications -> {
                            specifications.setIconUrl(productDTO.getImageUrl());
                            specificationsService.updateById(specifications);
                        });
            }
            return true;
        }
        return false;
    }
    /**
     * U查询id商品信息
     *
     * @author 乐昌新
     * @since 2024-10-12
     * @return list
     */
    @Override
    public List<Product> selectByCategoriesId(int id) {
        return baseMapper.selectByCategoriesId(id);
    }
    /**
     * U查询商品总数
     *
     * @author 乐昌新
     * @since 2024-10-12
     * @return list
     */
    @Override
    public String selectAllProductsCount() {
        return baseMapper.selectCount(null).toString();
    }

}
